﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using EZCRS.HCMS.Entity;
using EZCRS.HCMS.DataAcl;

using GFrame.Data;
using GFrame.Shadow;
using GFrame.Shadow.Search;

namespace EZCRS.HCMS.Bll.AIR
{
    public class InventoryPlan
    {
        private string CONNECTION_STRING_NAME = "mainDB";

        private ShadowSearch<HCRS_InventoryPlan> _search = new ShadowSearch<HCRS_InventoryPlan>();

        public InventoryPlan()
        {
            _search.ConnectionName = CONNECTION_STRING_NAME;
        }

        public HCRS_InventoryPlan GetItem(int ID)
        {

            List<HCRS_InventoryPlan> lst = _search.Search("ID = " + ID.ToString());

            if (lst.Count > 0) return lst[0];

            return null;
        }

        public List<HCRS_InventoryPlan> GetItemsByPID(int PID, int PageIndex, int PageSize, out int RecordCount, out int PageCount)
        {
            ShadowRule rule = new ShadowRule();

            rule.PageSize = PageSize;
            rule.PageIndex = PageIndex;

            List<HCRS_InventoryPlan> lst = _search.Search("PID = " + PID.ToString(), rule);

            RecordCount = rule.RecordCount;
            PageCount = rule.PageCount;

            return lst;
        }

        public List<HCRS_InventoryPlan> GetItemsByPID(int PID)
        {
            List<HCRS_InventoryPlan> lst = _search.Search("PID = " + PID.ToString());

            return lst;
        }

        public bool Apply(int ID)
        {
            AIR air = new AIR();

            HCRS_InventoryPlan plan = GetItem(ID);

            if (plan != null)
                return air.ApplyPlan(plan);
            
            
            return false;
        }

        public bool Delete(int ID)
        {
            HCRS_InventoryPlan plan = GetItem(ID);

            if (plan != null)
            {
                Shadow<HCRS_InventoryPlan> shadow = new Shadow<HCRS_InventoryPlan>(plan);

                shadow.ConnectionName = CONNECTION_STRING_NAME;

                return shadow.Delete();
            }

            return false;
        }


        public string IsCollision(int PID, List<String> RatePlans, DateTime StartDate, DateTime EndDate)
        {
            if (StartDate > EndDate) return string.Empty;

            bool bColl = true;

            DateTime dt1, dt2;

            using (InventoryPlanDAO dao = new InventoryPlanDAO())
            {

                foreach (string strPlan in RatePlans)
                {
                    string[] str = strPlan.Split('-');

                    if (str.Length > 1)
                    {
                        string strRoomCode = str[0];
                        string strPlanCode = str[1];

                        object min = dao.getPlanMinStartDate(PID, strRoomCode, strPlanCode);
                        object max = dao.getPlanMaxEndDate(PID, strRoomCode, strPlanCode);

                        if (min != null)
                            DateTime.TryParse(min.ToString(), out dt1);
                        else
                            return string.Empty;

                        if (max != null)
                            DateTime.TryParse(max.ToString(), out dt2);
                        else
                            return string.Empty;

                        if (StartDate < dt1 && EndDate < dt2 && EndDate < dt1)
                            bColl = false;

                        if (StartDate > dt1 && EndDate > dt2 && StartDate > dt2) bColl = false;

                        if (bColl)
                        {
                            dao.Dispose();

                            return strPlan;
                        }
                    }

                }
            }

            return string.Empty;
        }


        /// <summary>
        /// 检查房量计划是否有冲突
        /// </summary>
        /// <param name="PID"></param>
        /// <param name="RoomCode"></param>
        /// <param name="PlanCode"></param>
        /// <param name="StartDate"></param>
        /// <param name="EndDate"></param>
        /// <returns></returns>
        public bool IsCollision(int PID, string RoomCode, string PlanCode, DateTime StartDate, DateTime EndDate)
        {
            if (StartDate > EndDate) return false;

            bool bColl = true;

            DateTime dt1, dt2;

            using (InventoryPlanDAO dao = new InventoryPlanDAO())
            {

                object min = dao.getPlanMinStartDate(PID, RoomCode, PlanCode);
                object max = dao.getPlanMaxEndDate(PID, RoomCode, PlanCode);

                if (min != null)
                    DateTime.TryParse(min.ToString(), out dt1);
                else
                    return false;

                if (max != null)
                    DateTime.TryParse(max.ToString(), out dt2);
                else
                    return false;
            }

            if (StartDate < dt1 && EndDate < dt2 && EndDate < dt1)
                bColl = false;

            if (StartDate > dt1 && EndDate > dt2 && StartDate > dt2) bColl = false;

            return bColl;
        }


    }
}
